package com.graduationproject.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.graduationproject.common.R;
import com.graduationproject.entity.Permissions;
import com.graduationproject.entity.RelRolePerm;
import com.graduationproject.entity.dto.RolePermissionDTO;
import com.graduationproject.service.Impl.PermissionsServiceImpl;
import com.graduationproject.service.Impl.RelRolePermServiceImpl;
import jakarta.el.LambdaExpression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-08-20
 */
@RestController
@RequestMapping("api/permissions")
public class PermissionsController {
    @Autowired
    private PermissionsServiceImpl permissionsService;

    @Autowired
    private RelRolePermServiceImpl relRolePermService;

    /**
     * 根据角色id获取角色权限
     * @param id
     * @return
     */
    @GetMapping("/getRolePermissions/{id}")
    public R getRolePermissions(@PathVariable Integer id) {
        //根据角色id查询当前角色的权限，拿到权限id
        LambdaQueryWrapper<RelRolePerm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RelRolePerm :: getRoleId, id);
        List<RelRolePerm> list = relRolePermService.list(queryWrapper);
        RelRolePerm relRolePerm = list.get(0);
        String permissionId = relRolePerm.getPermissionId();
        String[] split = permissionId.split(",");
        List<String> list1 = Arrays.asList(split);
        return R.success(list1);
    }

    /**
     * 获取所有权限
     * @return
     */
    @GetMapping("/getAllPermissions")
    public R getAllPermissions() {
        List<Permissions> permissions = permissionsService.list();
        return R.success(permissions);
    }

    @PostMapping("/updateRolePermissions")
    public void updateRolePermissions(@RequestBody RolePermissionDTO rolePermissionDTO) {
        //根据角色id查询当前角色的权限，拿到权限id
        LambdaQueryWrapper<RelRolePerm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RelRolePerm :: getRoleId, rolePermissionDTO.getRoleId());
        List<RelRolePerm> list = relRolePermService.list(queryWrapper);
        List<Permissions> permissionData = rolePermissionDTO.getPermissionData();
        List<Integer> collect = permissionData.stream().map(item -> {
            return item.getId();
        }).collect(Collectors.toList());
        String string = collect.toString();
        string = string.substring(1, string.length() - 1);
        //删除每个元素之间的空格
        String result = string.replaceAll(",\\s+", ",");
        UpdateWrapper<RelRolePerm> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("role_id", rolePermissionDTO.getRoleId())
                .set("permission_id", result);
        relRolePermService.update(updateWrapper);
    }

}
